package 领扣算法.力扣计划.算法基础.全排列II;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

public class Main {
    LinkedList<List<Integer>> res=new LinkedList<>();
    HashSet<String> flag=new HashSet<>();
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        LinkedList<Integer> data=new LinkedList<>();
        res.add(new LinkedList<>());
        plus(nums,0,data,flag,new StringBuilder());
        return res;
    }

    private void plus(int[] nums, int i, LinkedList<Integer> data, HashSet<String> flag, StringBuilder stringBuilder) {
        if (flag.contains(stringBuilder.toString())){
            return;
        }
        if (i>=nums.length)
        {
            res.add(data);
            return;
        }
        else {
            flag.add(stringBuilder.toString());
        }
        int num=-1;
        for (;  i< nums.length ; i++) {
            if (num!=nums[i]) {
                LinkedList<Integer> integers = new LinkedList<>(data);
                integers.add(num);
                StringBuilder stringBuilder1 = new StringBuilder(stringBuilder);
                stringBuilder1.append(nums[i]);
                plus(nums,i+1,integers,flag,stringBuilder1);
                num=nums[i];
            }
        }
    }
}
